import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/login/index.dart';
import 'package:flutter_enjoy_plus/pages/notice/index.dart';
import 'package:flutter_enjoy_plus/pages/profile/index.dart';
import 'package:flutter_enjoy_plus/pages/tabs_page.dart';
import 'package:flutter_enjoy_plus/utils/tokenManager.dart';

import 'pages/404.dart';
import 'pages/house/house_list.dart';
import 'pages/house/location_list.dart';

void main(List<String> args) {
  runApp(MaterialApp(
    routes: {
      '/': (context) => const TabsPage(),
      '/notice_detail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
    },
    // 默认启动页面
    initialRoute: '/',
    // 路由拦截，在跳转时，访问的页面路径如果不是上面那三个，那么需要判断是否有token，没有则跳转登录页
    onGenerateRoute: (setting){
      // 获取token，判断token是否存在
      final token = tokenManager.getToken();
      // 如果token为空，则跳转登录页
      if (token.isEmpty) {
        return MaterialPageRoute(builder: (context) => const LoginPage(),);
      } else {
        // 如果token不为空，则判断当前页面是否为profile页面
        if (setting.name == '/profile') {
          return MaterialPageRoute(builder: (context) => ProfilePage(userInfo:setting.arguments as Map<String, dynamic>),);
        } else if (setting.name == '/house'){
          return MaterialPageRoute(builder: (context) => const HouseList());
        } else if (setting.name == '/add_house'){
          return MaterialPageRoute(builder: (context) => const LocationList());
        }
      }
      return MaterialPageRoute(builder: (context) => const NotFound());
    },
  ));
}